﻿using System;
using System.Diagnostics;
using System.Threading;
using ConsoleApp.Job.TaskFramework;
using ConsoleAppFramework.Util;

namespace ConsoleAppFramework.Job.TaskJob
{
    public class Tcp监控任务 : TaskBase
    {
        public override void Execute()
        {
            while (true)
            {
                Console.Write("当前tcp链接数总量:");
                ExecuteCommand("netstat -ant|find /C /I \"TCP\"");
                Console.Write("当前正在连接的连接数:");
                ExecuteCommand("netstat -an|find \"ESTABLISHED\" /c");
                ConsoleHelper.WriteInfoLine("");
                Thread.Sleep(1000);
            }
        }
        static void ExecuteCommand(string command)
        {
            var processInfo = new ProcessStartInfo()
            {
                FileName = "cmd.exe",
                Arguments = $"/C {command}",
                CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardOutput = true
            };

            using (var process = new Process())
            {
                process.StartInfo = processInfo;
                process.OutputDataReceived += (sender, e) => Console.Write(e.Data);
                process.Start();
                process.BeginOutputReadLine();
                process.WaitForExit();
            }
        }
    }
}
